package org.hyl.javabase.多线程;

//没有互斥锁的情况（可以自己跑跑看运行结果）：
public class lockdemo2 {

	private static Object lock = new Object();
	// static确保只有一把锁
	private int i = 0;

	public void increaseI() {
		synchronized (lock) {
			i=i+10;
			System.out.println(Thread.currentThread().getName() + "线程，i现在的值：" + i);
		}
	}

	public static void main(String[] args) {
		lockdemo2 ld = new lockdemo2();
		for (int i = 0; i < 1000; i++) {
			 new MyThread2(ld).start();
			// 所有线程共用一个LockDemo对象
		}
	}
}

class MyThread2 extends Thread {
	lockdemo2 ld;

	public MyThread2(lockdemo2 ld) {
		this.ld = ld;
	}

	public void run() {
		ld.increaseI();
	}
}
